package com.wondedDream.service.impl;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.wondedDream.model.CourseCategory;
import com.wondedDream.model.MockExam;
import com.wondedDream.model.MockExamQuestion;
import com.wondedDream.repository.CourseCategoryRepository;
import com.wondedDream.repository.MockExamQuestionRepository;
import com.wondedDream.repository.MockExamRepository;
import com.wondedDream.service.MockExamQuestionService;
import com.wondedDream.util.HtmlCrawler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class MockExamQuestionServiceImpl implements MockExamQuestionService {
    @Autowired
    private MockExamQuestionRepository mockExamQuestionRepository;

    @Autowired
    private MockExamRepository mockExamRepository;


    @Autowired
    private CourseCategoryRepository courseCategoryRepository;

    @Autowired
    private HtmlCrawler htmlCrawler;

    @Override
    public MockExamQuestion saveMockExamQuestion(MockExamQuestion question) {

        return mockExamQuestionRepository.save(question);
    }

    @Override
    public MockExamQuestion getMockExamQuestionById(Integer id) {
        return mockExamQuestionRepository.findById(id).orElse(null);
    }

    @Override
    public List<MockExamQuestion> getMockExamQuestionsByMockExamId(Integer mockExamId) {
        return mockExamQuestionRepository.findByMockExamId(mockExamId);
    }

    @Override
    public void deleteMockExamQuestion(Integer id) {
        mockExamQuestionRepository.deleteById(id);
    }

    @Override
    public MockExamQuestion updateMockExamQuestion(MockExamQuestion mockExamQuestion) {
        return mockExamQuestionRepository.saveAndFlush(mockExamQuestion);
    }

    @Override
    public void createMockExamQuestionByFileJson() {
        Gson gson = new Gson();
        String examJsonFile = htmlCrawler.parseMockExamJsonFile();

        List<MockExamQuestion> questionList = new ArrayList<>();


        CourseCategory courseCategory = new CourseCategory();
        courseCategory.setCategoryName("模考");
        CourseCategory courseCategory1 = courseCategoryRepository.save(courseCategory);


        MockExam mockExam = new MockExam();
        mockExam.setExamName("20241230");
        mockExam.setCategory(courseCategory1);
        MockExam mockExam1 = mockExamRepository.save(mockExam);

        JsonArray jsonArray = gson.fromJson(examJsonFile, JsonArray.class);
        for (JsonElement element : jsonArray) {
            JsonObject jsonObject = element.getAsJsonObject();
            String questionContent = jsonObject.get("question").getAsString();
            String correctAnswer = jsonObject.get("correctAnswer").getAsString();
            String analysis = jsonObject.get("analysis").getAsString();
            JsonArray optionsArray = jsonObject.getAsJsonArray("options");
            String optionA = optionsArray.get(0).getAsJsonObject().get("content").getAsString();
            String optionB = optionsArray.size() > 1? optionsArray.get(1).getAsJsonObject().get("content").getAsString() : "";
            String optionC = optionsArray.size() > 2? optionsArray.get(2).getAsJsonObject().get("content").getAsString() : "";
            String optionD = optionsArray.size() > 3? optionsArray.get(3).getAsJsonObject().get("content").getAsString() : "";
            MockExamQuestion question = null;
            if(correctAnswer.length() > 1){
                question = new MockExamQuestion(mockExam1, MockExamQuestion.QuestionType.MULTIPLE_CHOICE, questionContent, optionA, optionB, optionC, optionD, correctAnswer, analysis);
            }else {
                question = new MockExamQuestion(mockExam1, MockExamQuestion.QuestionType.SINGLE_CHOICE, questionContent, optionA, optionB, optionC, optionD, correctAnswer, analysis);
            }
            questionList.add(question);
        }
        mockExamQuestionRepository.saveAll(questionList);
    }
}